package fr.starn.meteo_widgets;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;



import fr.starn.meteo_widgets.JSonObject.DevicesProps;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ConfigureWidgetActivity extends Activity implements OnClickListener, IValueReceiver{

	private int widgetID;
	
	Context context;
	
	Button searchButton;
	TextView town;
	boolean isKelvin;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//manage widgetID
		widgetID = AppWidgetManager.INVALID_APPWIDGET_ID;
		Intent intent = getIntent();
	      Bundle extras = intent.getExtras();
	      if (extras != null) {
	         widgetID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
	       }
	      //No valid ID, so bail out.
	      if (widgetID == AppWidgetManager.INVALID_APPWIDGET_ID){
	          finish();
	      }
	      
	      
		setContentView(R.layout.configure_widget1);
		
		displayResultComponents(false);
		
		this.context=getApplicationContext();
		
		
	

		
		
		searchButton = (Button)findViewById(R.id.searchButton);
		searchButton.setOnClickListener(this);
		

		Button createButton = (Button)findViewById(R.id.create);
		createButton.setOnClickListener(this);
		
		
		
		
	}


	private void displayResultComponents(boolean isDisplayed){
		int visibility = isDisplayed?View.VISIBLE:View.INVISIBLE;
		
		findViewById(R.id.create).setVisibility(visibility);
		findViewById(R.id.country).setVisibility(visibility);
		findViewById(R.id.temp).setVisibility(visibility);
		findViewById(R.id.textView2).setVisibility(visibility);
	}
	


	@Override
	public void onClick(View view) {
		if ("searchButton".equals(view.getTag())){

			town = (TextView)findViewById(R.id.town);

			
			
			RadioGroup radioGroup = (RadioGroup)findViewById(R.id.unit);
		    int id= radioGroup.getCheckedRadioButtonId();
		    String unitStr="";
		    if (id!=-1){
			    View radioButton = radioGroup.findViewById(id);
			    int radioId = radioGroup.indexOfChild(radioButton);
			    if (radioId==-1){
					Toast.makeText(this.getApplicationContext(),"You must select a unit", Toast.LENGTH_LONG).show();
					return;
			    }
			    RadioButton btn = (RadioButton) radioGroup.getChildAt(radioId);
			    unitStr = (String) btn.getText().toString();
			    isKelvin = "Kelvin".equals(unitStr);
		    } else {
				Toast.makeText(this.getApplicationContext(),"You must select a unit", Toast.LENGTH_LONG).show();
				return;
		    }
			
			

		
		
			WidgetPropertiesBean props = new WidgetPropertiesBean(0,isKelvin,town.getText().toString());
			new Thread(new RestRequestHandler(context, this,props)).start();
		}
		if ("create".equals(view.getTag())){
			
			WidgetPropertiesBean props = new WidgetPropertiesBean(widgetID, isKelvin,town.getText().toString());
			WidgetPropertiesBean.saveToPreferences(this,props);
			
			Intent resultValue = new Intent();
		    resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
		    setResult(RESULT_OK, resultValue);
		    
	        Intent intent = new Intent(context, TempWidgetProvider.class);
	        intent.putExtra("props",props);
	        intent.setAction(TempWidgetProvider.SYNC_CLICKED);
	        try {
	        	PendingIntent.getBroadcast(context, props.getWidgetID(), intent, PendingIntent.FLAG_UPDATE_CURRENT).send();
	        } catch (Exception e){
	        	e.printStackTrace();
	        }
		    finish();
		}

		
	}


	@Override
	public void updateValue(Context contextNull, WidgetPropertiesBean props, DevicesProps result) {
		ConfigureWidgetActivity.this.runOnUiThread(new RefreshLabel(result));
	}

	
	class RefreshLabel implements Runnable{
		DevicesProps result;
		
		RefreshLabel(DevicesProps result){
			this.result=result;
		}
		
        @Override
        public void run() {
    		TextView country = (TextView)findViewById(R.id.country);
    		TextView temp = (TextView)findViewById(R.id.temp);
    		temp.setText("Temperature: "+result.getMain().getTemp(isKelvin));
    		country.setText("Country: "+result.getSys().getCountry());
    		displayResultComponents(true);
        }//public void run() {
		
	}


	@Override
	public void manageError(Context context, WidgetPropertiesBean props, String error) {
		Intent intent= new Intent(this.getApplicationContext(), ShowPopUpActivity.class);
		intent.putExtra("error", error);
		startActivity(intent);
	}
	

    


}
